package com.camelot.boot.server;

import com.google.gson.Gson;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ResumeExtractor1 {

    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    private static final String API_KEY = "sk-cc3f800cb24b4470871afd2923cdd571"; // 替换为你的DeepSeek API密钥

    /**
     * 从PDF文件中提取文本
     *
     * @param filePath PDF文件路径
     * @return 提取的文本内容
     * @throws IOException 如果文件读取或解析失败
     */
    public static String extractTextFromPDF(String filePath) throws IOException {
        PDDocument document =  null;
        PDFTextStripper pdfStripper = new PDFTextStripper();
        String text = pdfStripper.getText(document);
        document.close();
        return text;
    }

    /**
     * 构建DeepSeek API请求体
     *
     * @param text 简历文本内容
     * @return JSON格式的请求体
     */
    public static String buildRequest(String text) {
        Map<String, Object> request = new HashMap<>();
        request.put("prompt", "请从以下简历文本中提取姓名、联系方式、教育背景和工作经历等信息，并以结构化格式返回。\n" + text);
        request.put("max_tokens", 500000); // 根据需要调整最大返回token数
        Gson gson = new Gson();
        return gson.toJson(request);
    }

    /**
     * 发送请求到DeepSeek API
     *
     * @param requestBody JSON格式的请求体
     * @return API返回的响应内容
     * @throws IOException 如果请求发送或响应处理失败
     */
    public static String sendRequest(String requestBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(API_URL);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Authorization", "Bearer " + API_KEY);

        StringEntity entity = new StringEntity(requestBody, "UTF-8");
        httpPost.setEntity(entity);

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            System.out.println("2222: " + response);
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        }
    }

    /**
     * 处理API响应，提取所需信息
     *
     * @param response API返回的JSON响应
     * @return 提取的信息
     */
    public static String extractDataFromResponse(String response) {

        System.out.println("response111: " + response);
        Gson gson = new Gson();
        Map<String, Object> responseMap = gson.fromJson(response, Map.class);
        if (responseMap.containsKey("text")) {
            return (String) responseMap.get("text");
        }
        return "未获取到有效数据";
    }

    /**
     * 主方法，执行PDF简历信息抽取
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        String filePath = "D:\\new1\\ruoyi-vue-pro\\boot-server\\src\\main\\resources\\resume.pdf"; // 替换为你的PDF简历文件路径

        try {
            // 1. 提取PDF文本
          String text = extractTextFromPDF(filePath);
          //  String text = "请从以下简历文本中提取姓名、联系方式、教育背景和工作经历等信息，并以结构化格式返回";
            System.out.println("text: " + text);
            // 2. 构建请求体
            String requestBody = buildRequest(text);

            // 3. 发送请求并获取响应
            String response = sendRequest(requestBody);
            System.out.println("response: " + response);
            // 4. 处理响应并输出结果
            String result = extractDataFromResponse(response);
            System.out.println("提取结果: " + result);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}